package com.hopu.oa.controller.role;

import com.hopu.oa.entity.menu.AoaSysMenu;
import com.hopu.oa.entity.user.AoaRole;
import com.hopu.oa.service.impl.menu.menuServiceImpl;
import com.hopu.oa.service.impl.role.roleServiceImpl;
import com.hopu.oa.service.impl.user.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class roleManageController {
    @Autowired
    private roleServiceImpl roleService;
    @Autowired
    private menuServiceImpl menuService;
    @Autowired
    private UserServiceImpl userService;

    /**
     * 角色管理外层分页
     *
     * @param find
     * @param model
     * @param session
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/rolemanage")
    public String rolemanage(String find, Model model, HttpSession session, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size) {
        System.out.println("find = " + find);
        int totalElements = roleService.selectAllRole(find, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaRole> aoaRoles = roleService.selectAllRole(find, (page - 1) * size, size);
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        model.addAttribute("page", map1);
        model.addAttribute("rolelist", aoaRoles);
        model.addAttribute("find", find);
        model.addAttribute("url", "roletable");
        return "role/rolemanage";
    }

    /**
     * 角色管理内层分页
     *
     * @param find
     * @param model
     * @param session
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/roletable")
    public String roletable(String find, Model model, HttpSession session, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size) {

        int totalElements = roleService.selectAllRole(find, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaRole> aoaRoles = roleService.selectAllRole(find, (page - 1) * size, size);
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        model.addAttribute("page", map1);
        model.addAttribute("rolelist", aoaRoles);
        model.addAttribute("find", find);
        model.addAttribute("url", "roletable");
        return "role/roletable";
    }

    /**
     * 角色管理新增修改跳转
     *
     * @return
     */
    @RequestMapping("addrole")
    public String addrole(Model model, Integer roleId, Integer page) {
        if (roleId != null) {
            System.out.println("跳转修改");
            AoaRole aoaRole = roleService.selectRoleByRoleId(roleId);
            model.addAttribute("role", aoaRole);
            model.addAttribute("page", page);
            return "role/addrole";
        } else {
            System.out.println("跳转新增");
            AoaRole aoaRole = new AoaRole();
            model.addAttribute("role", aoaRole);
            return "role/addrole";
        }
    }

    /**
     * 角色新增修改
     *
     * @return
     */
    @RequestMapping("modifyrole")
    public String modifyrole(Model model, AoaRole aoaRole, Integer page) {
        AoaRole aoaRole1 = roleService.selectRoleByRoleName(aoaRole.getRoleName());
        AoaRole aoaRole2 = roleService.selectRoleByRoleId(aoaRole.getRoleId());
        if (aoaRole2 != null) {
            System.out.println("修改");
            if (aoaRole1 != null) {
                System.out.println("角色名称重复");
                model.addAttribute("errorMess", "角色名称重复");

                model.addAttribute("page", page);
                model.addAttribute("role", aoaRole2);

                return "role/addrole";
            } else {
                int i = roleService.updateRole(aoaRole);
                return "redirect:rolemanage?page=" + page;

            }
        } else {
            System.out.println("新增");
            if (aoaRole1 != null) {
                System.out.println("角色名称重复");
                AoaRole aoaRole5 = new AoaRole();
                model.addAttribute("role", aoaRole5);
                model.addAttribute("errorMess", "角色名称重复");
                model.addAttribute("page", page);
                return "role/addrole";
            } else {
                //新增role
                int i = roleService.isnertRole(aoaRole);
                //获取新增roleId
                Integer roleId = roleService.selectRoleByRoleName(aoaRole.getRoleName()).getRoleId();
                List<AoaSysMenu> aoaSysMenus = menuService.selectAllId();
                List<Integer> lists = new ArrayList<>();
                for (int i1 = 0; i1 <= aoaSysMenus.size() - 1; i1++) {
                    lists.add(aoaSysMenus.get(i1).getMenuId());
                }
                roleService.insertRoleList(lists, roleId);
                return "redirect:rolemanage";
            }
        }
    }


    /**
     * 跳转菜单权限设置界面
     *
     * @param model
     * @param roleId
     * @return
     */
    @RequestMapping("roleset")
    public String roleset(Model model, Integer roleId) {
        List<AoaSysMenu> aoaSysMenus1 = menuService.findAllOne(roleId);
        List<AoaSysMenu> aoaSysMenus2 = menuService.findAllTwo(roleId);
        AoaRole aoaRole = roleService.selectRoleByRoleId(roleId);
        model.addAttribute("role", aoaRole);
        model.addAttribute("findoneMenuAll", aoaSysMenus1);
        model.addAttribute("findtwoMenuAll", aoaSysMenus2);
        return "role/roleset";
    }

    /**
     * 修改访问菜单权限
     *
     * @param roleId
     * @param menuId
     * @param isShow
     * @param model
     * @return
     */
    @RequestMapping("upateOneMenuRole")
    public String upateOneMenuRole(Integer roleId, Integer menuId, Integer isShow, Model model) {
        System.out.println("roleId = " + roleId);
        System.out.println("menuId = " + menuId);
        System.out.println("isShow = " + isShow);

        if (isShow == 1) {
            int i = menuService.updateRoleMenu(roleId, menuId, 0);
            List<AoaSysMenu> aoaSysMenus = menuService.updateOne(roleId, menuId);
            for (int i1 = 0; i1 < aoaSysMenus.size(); i1++) {
                int i2 = menuService.updateTwo(aoaSysMenus.get(i1).getMenuId(), roleId, 0);
            }
            List<AoaSysMenu> aoaSysMenus1 = menuService.findAllOne(roleId);
            List<AoaSysMenu> aoaSysMenus2 = menuService.findAllTwo(roleId);
            AoaRole aoaRole = roleService.selectRoleByRoleId(roleId);
            //查询当前一级菜单下的二级菜单
            model.addAttribute("role", aoaRole);
            model.addAttribute("findoneMenuAll", aoaSysMenus1);
            model.addAttribute("findtwoMenuAll", aoaSysMenus2);
            return "role/roleset";
        }
        if (isShow == 0) {
            int i = menuService.updateRoleMenu(roleId, menuId, 1);
            List<AoaSysMenu> aoaSysMenus1 = menuService.findAllOne(roleId);
            List<AoaSysMenu> aoaSysMenus2 = menuService.findAllTwo(roleId);
            AoaRole aoaRole = roleService.selectRoleByRoleId(roleId);
            model.addAttribute("role", aoaRole);
            model.addAttribute("findoneMenuAll", aoaSysMenus1);
            model.addAttribute("findtwoMenuAll", aoaSysMenus2);
            return "role/roleset";
        }

        return "role/roleset";
    }

    @RequestMapping("upateTwoMenuRole")
    public String upateTwoMenuRole(Integer roleId, Integer menuId, Integer isShow, Model model) {
        System.out.println("roleId = " + roleId);
        System.out.println("menuId = " + menuId);
        System.out.println("isShow = " + isShow);

        if (isShow == 1) {
            int i = menuService.updateRoleMenu(roleId, menuId, 0);
            List<AoaSysMenu> aoaSysMenus1 = menuService.findAllOne(roleId);
            List<AoaSysMenu> aoaSysMenus2 = menuService.findAllTwo(roleId);
            AoaRole aoaRole = roleService.selectRoleByRoleId(roleId);
            model.addAttribute("role", aoaRole);
            model.addAttribute("findoneMenuAll", aoaSysMenus1);
            model.addAttribute("findtwoMenuAll", aoaSysMenus2);
            return "role/roleset";
        }
        if (isShow == 0) {
            int i = menuService.updateRoleMenu(roleId, menuId, 1);
            List<AoaSysMenu> aoaSysMenus1 = menuService.findAllOne(roleId);
            List<AoaSysMenu> aoaSysMenus2 = menuService.findAllTwo(roleId);
            AoaRole aoaRole = roleService.selectRoleByRoleId(roleId);
            model.addAttribute("role", aoaRole);
            model.addAttribute("findoneMenuAll", aoaSysMenus1);
            model.addAttribute("findtwoMenuAll", aoaSysMenus2);
            return "role/roleset";
        }

        return "role/roleset";
    }


    /**
     * 删除数据
     *
     * @param roleId 主键
     * @return 新增结果
     */
    @RequestMapping("deshan")
    public String delete(Integer roleId, Model model, Integer page) {
        System.out.println("roleId = " + roleId);
        int size = userService.selectByRole(roleId).size();
        System.out.println(size);
        if (size > 0) {
            System.out.println("不能删除");
            //错误提示
            model.addAttribute("errorMess", "该职位存在职员，不能删除");
            return "redirect:rolemanage?page=" + page;
        } else {
            System.out.println("可以删除");
            int i = roleService.deleteRoleAndRoleList(roleId);
            System.out.println("i = " + i);
            return "redirect:rolemanage?page=" + page;
        }
    }


}
